package com.heriparid.slideshow.sound;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;

public class SoundManager {
	private final  SoundPool mSoundPool;
	private final HashMap<Integer, Integer> mSoundPoolMap;
	private final AudioManager  mAudioManager;
	private final Context mContext;
	
	public SoundManager(Context context) {
		mContext = context;
		mSoundPoolMap = new HashMap<Integer, Integer>();
		mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
		mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);		
	}	
	
	public void addSound(int soundResourceId) {
		if(!mSoundPoolMap.containsKey(soundResourceId)){
			mSoundPoolMap.put(soundResourceId, mSoundPool.load(mContext, soundResourceId, 1));
		}
	}
	
	public void playSound(int index) {
		float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume	/ mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);		
	}
	
	public void clearSoundPool(){
		mSoundPoolMap.clear();
		mSoundPool.release();		
	}

	/*public void playLoopedSound(int index) {
		float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume	/ mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
	}*/

}
